<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){

        if(S('hotQues')){
           $this->assign('hotQues',S('hotQues')); 
        }else{
            $hotQues = M('question')->field('qid,title,answer_num')->order("answer_num desc")->limit(10)->select();
            S('hotQues',$hotQues,600);
            $this->assign('hotQues',S('hotQues')); 
        }


    	$hotUser = M('user')->field('face,nickname,uid,answer_num')->order("answer_num desc")->limit(12)->select();


    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.uid inner join category t3 on t1.cid=t3.cid where t1.top='1'";

    	$topQues = M()->query($sql);

    	$this->assign('topQues',$topQues);


    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.uid inner join category t3 on t1.cid=t3.cid where t1.top<>'1' order by create_time desc limit 5";

    	$question = M()->query($sql);

    	$this->assign('question',$question);
        $this->assign('title',"一个解决老司机对象的论坛");
    	$this->assign('hotUser',$hotUser);
        $this->display();
    }

    public function all()
    {
    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.uid inner join category t3 on t1.cid=t3.cid where t1.top='1'";

    	$topQues = M()->query($sql);

    	$this->assign('topQues',$topQues);



		$count      = M('question')->where('top<>1')->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,4);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.uid inner join category t3 on t1.cid=t3.cid where t1.top<>'1' order by create_time desc limit ".$Page->firstRow.",".$Page->listRows;

    	$question = M()->query($sql);

    	$this->assign('question',$question);
        $this->assign('title',"全部帖子");
		$this->assign('page',$show);// 赋值分页输出
    	$this->display();
    }

    public function unsolved($value='')
    {
    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.uid inner join category t3 on t1.cid=t3.cid where t1.top<>'1' and t1.status='未结贴' order by create_time desc";

    	$question = M()->query($sql);
        $this->assign('title',"未解决");
    	$this->assign('question',$question);
    	$this->display('all');
    }
}